package com.wuxinwudai.android.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


/**
 * ImageViewPagerAdapter 为图片试图分页器
 * @author 吾心无待 于 2016年05月30日
 */
public class ImageViewPagerAdapter extends PagerAdapter {
    protected int[] mDrawables;//图片 Drawable 资源
    protected Context mContext;//上下文对象

    /**
     * 构造函数，初始化 GuidePagerAdapter 类的一个新实例
     * @param context 上下文对象
     * @param drawables 图片资源数组
     */
    public ImageViewPagerAdapter(Context context,int[] drawables){
        this.mContext = context;
        this.mDrawables = drawables;
    }

    @Override
    public int getCount() {
        return mDrawables.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView iv = new ImageView(mContext);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setImageResource(mDrawables[position]);
        container.addView(iv);
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
